<?php

// Europa100AD
//
// Database Connection - Aug 2007
//
// To be inclued in every file.
//

$query_count = 0;

class database extends mysqli
{
    // Redefine the parent method
    function query($query)
    {
        global $query_count,$query_log,$config;
        $query_count++;
        if ($config['query_logging'])
        {
            $query_log .= '<li>'.$query;
            log_memory('MySQL -> '.$query);
        }
        //echo "<br>counted query: total $query_count<br>\n";
        $result = parent::query($query);
        if($result === FALSE)
        {
           die("Query failed: $query <br><br>\n<b>$this->error</b>");
        } 
        return $result;
    }
    
    function return_first_result($query)
      {
        
        //
        // EXAMPLE USAGE
        //
        //$test = $db->return_first_result("SELECT * FROM user_sleep");
        //echo $test['assoc_id'];
        $result = $this->query($query);
        if($result == FALSE)
        {
           die("Query failed: $query <br>\n".$this->error);
        } 
        return $result->fetch_assoc();
      }
      
      function fetch_real_array($query,$index = '')
        {
            // declare the array
            $return = array();
            if ($query)
            {
                $fetch = $this->query($query);
                while ($row = $fetch->fetch_assoc())
                {
                    if (strlen($index) > 0)
                    {
                        $sort_index = $row["$index"];
                        $return[$sort_index] = $row;
                    }
                    else
                    {
                        $return[] = $row;
                    }
                }
                $fetch->free_result();
             return $return;
            }
        } 
}

$db = new database("localhost", "europa100ad_db", "", "europa100ad_db"); 

/* check connection */

# Check the connection and print an error and stop the page if it can't connect
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

//printf("Host information: %s\n", $database->host_info);
 

?>